package com.gwnet.rider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwnet.common.core.exception.base.BaseException;
import com.gwnet.common.satoken.utils.LoginHelper;
import com.gwnet.quartz.util.BeanUtils;
import com.gwnet.rider.domain.RiderResignation;
import com.gwnet.rider.domain.RiderUser;
import com.gwnet.rider.domain.bo.RiderResignationApplyBO;
import com.gwnet.rider.domain.bo.RiderResignationPageBO;
import com.gwnet.rider.enums.RiderResignationAuditStatusEnum;
import com.gwnet.rider.enums.RiderStatusEnum;
import com.gwnet.rider.mapper.RiderResignationMapper;
import com.gwnet.rider.mapper.RiderUserMapper;
import com.gwnet.rider.service.RiderResignationService;
import com.gwnet.system.api.model.LoginUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 骑手离职表 服务实现类
 * </p>
 *
 * @author WangLiang
 * @since 2024-08-09
 */
@AllArgsConstructor
@Service
public class RiderResignationServiceImpl extends ServiceImpl<RiderResignationMapper, RiderResignation> implements RiderResignationService {


    private final RiderResignationMapper riderResignationMapper;
    private final RiderUserMapper riderUserMapper;


    @Override
    public Boolean applyResignation(RiderResignationApplyBO applyBO) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        RiderResignation saveData = new RiderResignation();
        // 申请离职，填写表单
        BeanUtils.copyProperties(applyBO,saveData);
        saveData.setAuditStatus(RiderResignationAuditStatusEnum.UN_COMMIT.code());
        saveData.setRiderId(loginUser.getUserId());
        saveData.setRiderName(loginUser.getUsername());
        int count = riderResignationMapper.insert(saveData);
        return count > 0;
    }

    @Override
    public Boolean submit(RiderResignation resignation){
        // 状态改为提交
        RiderResignation riderResignation = riderResignationMapper.selectById(resignation.getResignationId());
        check(resignation,RiderResignationAuditStatusEnum.UN_COMMIT.code());

        riderResignation.setAuditStatus(RiderResignationAuditStatusEnum.AUDITING.code());
        int count = riderResignationMapper.updateById(riderResignation);
        return count > 0;
    }

    @Override
    public Boolean pass(RiderResignation resignation) {

        // 状态改为审核通过
        RiderResignation riderResignation = riderResignationMapper.selectById(resignation.getResignationId());
        check(resignation,RiderResignationAuditStatusEnum.AUDITING.code());

        riderResignation.setAuditStatus(RiderResignationAuditStatusEnum.SUCCESS.code());
        int count = riderResignationMapper.updateById(riderResignation);
        // 骑手账号禁用
        RiderUser user = riderUserMapper.selectById(riderResignation.getRiderId());
        user.setStatus(RiderStatusEnum.RESIGNATION.code());
        riderUserMapper.updateById(user);

        return count > 0;
    }

    @Override
    public Page<RiderResignation> page(RiderResignationPageBO pageBO) {
        Page<RiderResignation> page = Page.of(pageBO.getCurrent(), pageBO.getSize());
        LambdaQueryWrapper<RiderResignation> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByDesc(RiderResignation::getCreateTime);
        Page<RiderResignation> pageResult = riderResignationMapper.selectPage(page, queryWrapper);
        return pageResult;
    }


    /**
     * 前置检查
     * @param riderResignation
     * @param auditCode
     */
    private void check(RiderResignation riderResignation,Integer auditCode){
        if(ObjectUtils.isEmpty(riderResignation)){
            throw new BaseException("审核单不存在");
        }
        if(auditCode.equals(riderResignation.getAuditStatus())){
            throw new BaseException("只有"+ RiderResignationAuditStatusEnum.instance(auditCode).desc() + "的审核单才可以提交");
        }
    }
}
